<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require "include/config.inc.php";

Class DB_Manager {
    private $mysqli;
    private $conn;
    private static $instance = null;

    public static function getInstance() {
        return self::$instance == null ? self::$instance = new DB_Manager() : self::$instance;
    }

    private function __construct() {
        $this->mysqli = mysqli_connect(Config::$db_host, Config::$db_user, Config::$db_pass, Config::$db_name) or die("Error opening db " . mysqli_error($this->conn));
    }

    public function close() {
        if ($this->mysqli != NULL) {
            $this->mysqli->close() or die("Error closing db " . mysqli_error($conn));
        }
    }
    
    public function getConnection(){
        return $this->mysqli;
    }

    public static function getLastErrorCode(){
        return self::$instance->mysqli->errno;
    }
    public static function getLastInsertedId(){
        return self::$instance->mysqli->insert_id;
    }

    public function query($query) {
        return $this->mysqli->query($query);
    }

    public function getCodeError() {
        return $this->mysqli->errno . " - " . $this->mysqli->error;
    }

    public function __destruct() {
        $this->close();
    }

    public static function queryInsert($table, $arr) {
        $start = "INSERT INTO " . $table;
        $keys = " (";
        $values = " VALUES (";
        foreach ($arr as $key => $value) {
            $keys = $keys . '`' . $key . '`,';
            $value=str_replace('\'',"\\'",$value);
            $values = $values . "'" . $value . "',";
        }
        $keys = rtrim($keys, ",") . ')';
        $values = rtrim($values, ",") . ')';
        return $start . $keys . $values;
    }

    public static function queryUpdateEasy($table, $arr, $cond) {
        $query = "UPDATE " . $table . " SET ";
        foreach ($arr as $key => $value) {
            $value=str_replace('\'',"\\'",$value);
            $query = " " . $query . $key . " = '" . $value . "', ";
        }
        $query = rtrim($query, ", ") . " WHERE ";
        foreach ($cond as $key => $value) {
            $value=str_replace('\'',"\\'",$value);
            $query = $query . " " . $key . "= '" . $value . "' AND";
        }
        $query = rtrim($query, "AND");
        return $query;
    }

    public static function queryDeleteEasy($table, $cond) {
        $query = "DELETE FROM " . $table;
        $query = $query . " WHERE ";
        foreach ($cond as $key => $value) {
            $value=str_replace('\'',"\\'",$value);
            $query = $query . " " . $key . "= '" . $value . "' AND";
        }
        $query = rtrim($query, "AND");
        return $query;
    }

    public static function querySelectEasy($table, $arr, $cond) {
        $query = "SELECT ";
        foreach ($arr as $key => $value) {
            $query = $query . $key . ", ";
        }
        $query = rtrim($query, ", ") . " FROM " . $table . " WHERE ";
        foreach ($cond as $key => $value) {
            $value=str_replace('\'',"\\'",$value);
            $query = " " . $query . $key . "= '" . $value . "' AND";
        }
        $query = rtrim($query, "AND");
        return $query;
    }
    
    public function queryEscape($string){
        return $this->mysqli->real_escape_string($string);
    }
    
}

?>